package es.usj.apps.moviles.utils;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;

public class GPSVehicle implements LocationListener {

	public LocationManager manager = null;
	private MapActivity view;

	public interface OnGPSManagerListener {
		public void onGPSManager(GeoPoint gpsPosition, String address,
				MapActivity view);
	}

	OnGPSManagerListener mListenerGPS = null;

	public void setOnGPSManagerListener(OnGPSManagerListener listener) {
		mListenerGPS = listener;
	}

	public interface OnGPSManagerStatus {
		public void onGPSManagerStatus(String status);
	}

	OnGPSManagerStatus mStatusGPS = null;

	public void setOnGPSManagerStatus(OnGPSManagerStatus listener) {
		mStatusGPS = listener;
	}

	public GPSVehicle(MapActivity view) {
		manager = (LocationManager) view
				.getSystemService(Context.LOCATION_SERVICE);
		this.view = view;
	}

	@Override
	public void onLocationChanged(Location location) {
		//Log.v("Electric-Vehicle", "Location Changed");

		Geocoder geo = new Geocoder(view.getBaseContext());
		List<Address> address = null;

		try {
			address = geo.getFromLocation(location.getLatitude(),
					location.getLongitude(), 1);
		} catch (IOException e) {
			e.printStackTrace();
		}

		String finalAddress = "";

		if (address.size() > 0) {
			Address item = (Address) address.get(0);

			if (item.getFeatureName() != null) {
				finalAddress += item.getFeatureName().replace("null", "");
			}

			if (item.getLocality() != null) {
				finalAddress += ", "
						+ item.getLocality().replace("null", "");
			}

			if (item.getCountryName() != null) {
				finalAddress += ", "
						+ item.getCountryName().replace("null", "");
			}
		}

		GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1E6),
				(int) (location.getLongitude() * 1E6));

		Log.v("Electric-Vehicle", "Location Changed: " + point.getLatitudeE6()
				+ ", " + point.getLongitudeE6());

		if (mListenerGPS != null)
			mListenerGPS.onGPSManager(point, finalAddress, this.view);
	}

	@Override
	public void onProviderDisabled(String arg0) {
		Log.v("Electric-Vehicle", "Disabled");

		/* bring up the GPS settings */
		Intent intent = new Intent(
				android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		this.view.startActivity(intent);
	}

	@Override
	public void onProviderEnabled(String arg0) {
		Log.v("Electric-Vehicle", "Enabled");
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {

		String result = "";

		switch (status) {
		case LocationProvider.OUT_OF_SERVICE:
			result = "Status Changed: Out of Service";

			break;
		case LocationProvider.TEMPORARILY_UNAVAILABLE:
			result = "Status Changed: Temporarily Unavailable";

			break;
		case LocationProvider.AVAILABLE:
			result = "Status Changed: Available";

			break;
		}

		if (mStatusGPS != null) {
			mStatusGPS.onGPSManagerStatus(result);
		}
	}
}
